//冒泡排序--实现一个对整形数组的冒泡排序
#include <stdio.h>
#include <stdlib.h>

//冒泡排序(从后向前，升序)
void bubbleSort(int arr[],int size) {
	//bound是已排序与未排序的中间值
	int bound = 0; 
	for ( ; bound < size; bound++) {
		//cur是当前元素的下标 (令 cur=size-1 是因为数组的最后一个元素的下标为size-1)
		for (int cur = size - 1; cur > bound; cur--) {
			//若前一个数大于后一个数，则交换他们的位置
			if (arr[cur-1] > arr[cur]) { 
				int temp = arr[cur];
				arr[cur] = arr[cur - 1];
				arr[cur-1] = temp;
			}
		}
	}
}

//冒泡排序(从前向后，升序)
void bubbleSort2(int arr2[], int size2) {
	int bound2 = 0;
	for (; bound2 < size2 ; bound2++) {
		for (int cur = 0 ; cur < size2 ; cur++) {
			if (arr2[cur - 1] > arr2[cur]) {
				int temp2 = arr2[cur - 1];
				arr2[cur - 1] = arr2[cur];
				arr2[cur] = temp2;
			}
		}
	}
}

int main() {
	//从后向前进行冒泡排序
	int arr[] = { 1, 8, 0, 4, 7, 3, 9, 2, 5, 6 };
	int size = sizeof(arr) / sizeof(arr[0]);
	bubbleSort(arr,size);
	printf("从后向前进行冒泡排序，结果为：");
	for (int i = 0; i < size; i++) {
		printf("%d ", arr[i]);
	}
	printf("\n\n");

	//从前向后进行冒泡排序
	int arr2[] = { 12, 15, 19, 11, 10, 14 };
	int size2 = sizeof(arr2) / sizeof(arr2[0]);
	bubbleSort2(arr2, size2);
	printf("从前向后进行冒泡排序，结果为：");
	for (int i = 0; i < size2; i++) {
		printf("%d ", arr2[i]);
	}
	printf("\n\n");


	system("pause");
	return 0;
}